//
//  UIImage_Ex.swift
//  DawnFoundation
//
//  Created by dawn on 2021/8/26.
//

import UIKit

public extension UIImage {
    
    static func createImg(color: UIColor,
                          rect: CGRect) -> UIImage? {
        if rect.equalTo(.zero) {return nil}
        UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
        let ctx = UIGraphicsGetCurrentContext()
        color.set()
        ctx?.fill(rect)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }
    
    ///绘制纯色背景圆角
    static func drawRectWithRoundedCorner(radius: CGFloat,
                                              borderWidth: CGFloat,
                                              backgroundColor: UIColor,
                                              borderColor: UIColor?,
                                              size:CGSize) -> UIImage {
        let sizeToFit = CGSize(width: size.width, height: size.height)
        let halfBorderWidth = CGFloat(borderWidth / 2.0)
        
        UIGraphicsBeginImageContextWithOptions(sizeToFit, false, UIScreen.main.scale)
        let ctx = UIGraphicsGetCurrentContext()
        borderWidth > 0.0 ? ctx?.setLineWidth(borderWidth) : nil
        borderColor != nil ? ctx?.setStrokeColor(borderColor!.cgColor) : nil
        ctx?.setFillColor(backgroundColor.cgColor)
        
        let width = sizeToFit.width, height = sizeToFit.height
        ctx?.move(to: CGPoint(x: width - halfBorderWidth, y: radius + halfBorderWidth))  // 开始坐标右边开始
        ctx?.addArc(tangent1End: CGPoint(x:width - halfBorderWidth, y:height - halfBorderWidth), tangent2End: CGPoint(x:width - radius - halfBorderWidth, y:height - halfBorderWidth), radius: radius)  // 右下角角度
        ctx?.addArc(tangent1End: CGPoint(x:halfBorderWidth, y:height - halfBorderWidth), tangent2End: CGPoint(x:halfBorderWidth, y:height - radius - halfBorderWidth), radius: radius)// 左下角角度
        ctx?.addArc(tangent1End: CGPoint(x:halfBorderWidth, y:halfBorderWidth), tangent2End: CGPoint(x:width - halfBorderWidth, y:halfBorderWidth), radius: radius) // 左上角
        ctx?.addArc(tangent1End: CGPoint(x:width - halfBorderWidth, y:halfBorderWidth), tangent2End: CGPoint(x:width - halfBorderWidth, y:radius + halfBorderWidth), radius: radius) // 右上角
        ctx?.drawPath(using: .fillStroke)
        let output = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return output!
    }
    
    ///图片裁剪圆角
    func corner(radius: CGFloat = 5,
                corner: UIRectCorner = .allCorners,
                sizeToFit: CGSize = .zero) -> UIImage {
        let sizeToFit = (sizeToFit == .zero) ? size : sizeToFit
        let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: sizeToFit)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
        let ctx = UIGraphicsGetCurrentContext()
        ctx?.addPath(UIBezierPath(roundedRect: rect,
                                  byRoundingCorners: UIRectCorner.allCorners,
                                  cornerRadii: CGSize(width: radius, height: radius)).cgPath)
        ctx?.clip()
        self.draw(in: rect)
        ctx?.drawPath(using: .fillStroke)
        let output = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return output!
    }
    
    func maxShowSize(_ maxSize: CGSize,
                     _ minSize: CGSize)-> CGSize {
        var w = size.width
        var h = size.height
        
        if (minSize.width > w || minSize.height > h) {
            if (w/h < minSize.width/minSize.height) {
                w = minSize.height/h * minSize.width;
                h = minSize.height;
            }else {
                h = minSize.width/w * minSize.height;
                w = minSize.width;
            }
        }
        
        if (w/h < maxSize.width/maxSize.height) {
            if (maxSize.height < h) {
                w = w / h * maxSize.height;
                h = maxSize.height;
            }
        }else {
            if (maxSize.width < w) {
                h = h / w * maxSize.width;
                w = maxSize.width;
            }
        }
        return CGSize(width: w, height: h)
    }

    
}
extension UIImageView {
    
    
    
    func corner(radius: CGFloat = 5,
                corner: UIRectCorner = .allCorners) {
        image = image?.corner(radius: radius,
                              corner: corner,
                              sizeToFit: CGSize(width: bounds.size.width * UIScreen.main.scale,
                                                height: bounds.size.width * UIScreen.main.scale))
    }
    
    
}
